package com.fuqiang.nightshopadmin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fuqiang.nightshopadmin.model.UmsMenuNode;
import com.fuqiang.nightshopadmin.service.IUmsMenuService;
import com.fuqiang.nightshopcommon.api.R;
import com.fuqiang.nightshopumsmodel.entity.UmsMenu;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 后台菜单表 前端控制器
 * </p>
 *
 * @author fuqiang
 * @since 2020-04-14
 */
@RestController
@RequestMapping("/menu")
@AllArgsConstructor
public class UmsMenuController {

    private IUmsMenuService umsMenuService;

    @ApiOperation("分页查询后台菜单")
    @RequestMapping(value = "/list/{parentId}", method = RequestMethod.GET)
    @ResponseBody
    public R<IPage<UmsMenu>> list(@PathVariable Long parentId,
                                  @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        IPage<UmsMenu> menuList = umsMenuService.list(parentId, pageSize, pageNum);
        return R.success(menuList);
    }

    @ApiOperation("根据ID获取菜单详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public R<UmsMenu> getItem(@PathVariable Long id) {
        UmsMenu umsMenu = umsMenuService.getById(id);
        return R.success(umsMenu);
    }

    @ApiOperation("修改后台菜单")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> update(@PathVariable Long id, @RequestBody UmsMenu umsMenu) {
        int count = umsMenuService.update(id, umsMenu);
        if (count > 0) {
            return R.success(count);
        } else {
            return R.failed();
        }
    }

    @ApiOperation("添加后台菜单")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public R<Integer> create(@RequestBody UmsMenu umsMenu) {
        int count = umsMenuService.create(umsMenu);
        if (count > 0) {
            return R.success(count);
        } else {
            return R.failed();
        }
    }

    @ApiOperation("树形结构返回所有菜单列表")
    @RequestMapping(value = "/treeList", method = RequestMethod.GET)
    @ResponseBody
    public R<List<UmsMenuNode>> treeList() {
        List<UmsMenuNode> list = umsMenuService.treeList();
        return R.success(list);
    }
}

